package org.apache.struts2.plugin.json.writer;

import org.apache.struts2.plugin.json.model.JSONValue;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Arrays;
import org.junit.Test;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import static org.junit.Assert.*;

/**
 * @author VERDOÏA Laurent <verdoialaurent@gmail.com>
 */
@RunWith(Parameterized.class)
public abstract class AbstractWriterTester {

    protected JSONValue<? extends Object> content;
    protected String expected;
    protected ByteArrayOutputStream byteStream;
    protected OutputStream printStream;

    public AbstractWriterTester(JSONValue<? extends Object> content,
            String expected) {
        this.content = content;
        this.expected = expected;
    }

    @Before
    public void before() {
        byteStream = new ByteArrayOutputStream();
        printStream = new PrintStream(byteStream);
    }

    @Test
    public final void write() throws Exception {
        AbstractJSONWriter j = JSONWriterFactory.create(content, printStream);
        j.write();

        if (!Arrays.equals(expected.getBytes(), byteStream.toByteArray())) {
            String result = byteStream.toString();
            System.out.println("Expected: " + expected);
            System.out.println("Result  : " + result);
            assertEquals(result, expected);
        }
    }
}
